const config = require('../config');
const { ethers } = require('ethers');
const attestationApi = config.attestationApi.url;
const axios = require('axios');

const Web3 = require('web3');

let txHash = "0x43f17e86f1932de6491b1465bb561150e3a62bef8c9261e6c3622a3e99efd70c";

let url = config.goerli.urls[1];

const mint = require('./4_noble_mint_usdc');
const run = async ()=>{
    let web3 = new Web3(new Web3.providers.HttpProvider(url));
    const eventTopic = web3.utils.keccak256('MessageSent(bytes)');

    const transactionReceipt = await web3.eth.getTransactionReceipt(txHash);
    const log = transactionReceipt.logs.find((l) => l.topics[0] === eventTopic)
    console.log("log : ", log);
    const messageBytes = web3.eth.abi.decodeParameters(['bytes'], log.data)[0]
    console.log("messageBytes: ", messageBytes);
    const messageHash = web3.utils.keccak256(messageBytes);


    console.log('messageHash: ', messageHash);
    let circleUrl = attestationApi + messageHash;
    console.log('circleUrl: ', circleUrl);
   /*
    const res = await axios.get(circleUrl);
    console.log('res.data', res.data);

    const attestation = res.data.attestation;
    const message = messageBytes;

    const buffer = Buffer.from(attestation, "hex")

    await mint(messageBytes, attestation)*/



}

run();

/*
res.data {
  attestation: '0xaf16f1c051310000d6b0204558d026e58ac5dbe607faed2723a1a3378859046d20367faeba595ffb371567fda922a6d0e5c938555e9a1cec0261f8d63ce126261b9799b96c0abb2627b8090b2f455e318530bd496b58cb36c47a46f0f512663cad71a47bd25614e2d947c65d3259201ca47e85164fce0a421f47e01bcbda3c4a191c',
  status: 'complete'
}

* */